package com.example.cyw.news;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity implements Jump {
    private  String yuleUrl="http://v.juhe.cn/toutiao/index?type=yule&key=fcfbc4f00966c4e14b5caf5dd710021a";
    private  String topUrl="http://v.juhe.cn/toutiao/index?type=top&key=fcfbc4f00966c4e14b5caf5dd710021a";
    private  String kejiUrl="http://v.juhe.cn/toutiao/index?type=keji&key=fcfbc4f00966c4e14b5caf5dd710021a";
    private  String caijingUrl="http://v.juhe.cn/toutiao/index?type=caijing&key=fcfbc4f00966c4e14b5caf5dd710021a";
    private  String tiyuUrl="http://v.juhe.cn/toutiao/index?type=ytiyu&key=fcfbc4f00966c4e14b5caf5dd710021a";
    private TextView tv_top,tv_yule,tv_tiyu,tv_keji,tv_caijing;
    private RecyclerView listView;
    private MyAdapter myAdapter;

    private Handler handler;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initview();
       initHadler();

            getDataFromapi(topUrl);
        tv_top.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getDataFromapi(topUrl);
            }
        });
        tv_yule.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getDataFromapi(yuleUrl);
            }
        });
        tv_tiyu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getDataFromapi(tiyuUrl);
            }
        });
        tv_keji.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getDataFromapi(kejiUrl);
            }
        });
        tv_caijing.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getDataFromapi(caijingUrl);
            }
        });









    }

    private void initHadler() {
        handler=new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                if (msg.what==1000){
                   News news= (News) msg.obj;
                    myAdapter=new MyAdapter(MainActivity.this,news,MainActivity.this);
                    myAdapter.notifyDataSetChanged();
                    listView.setAdapter(myAdapter);
                }
                return false;
            }
        });
    }

    private void initview() {
        tv_top= (TextView) findViewById(R.id.tv_top);
        tv_yule= (TextView) findViewById(R.id.tv_yule);
        tv_tiyu= (TextView) findViewById(R.id.tv_tiyu);
        tv_keji= (TextView) findViewById(R.id.tv_keji);
        tv_caijing= (TextView) findViewById(R.id.tv_caijing);
        listView= (RecyclerView) findViewById(R.id.listView);
        LinearLayoutManager layoutManager=new LinearLayoutManager(this);
        listView.setLayoutManager(layoutManager);


    }

    private void getDataFromapi(String url) {
        Log.e("getData","被调用");
        OkHttpClient client=new OkHttpClient();
        Request request=new Request.Builder()
                .url(url)
                .build()
                ;
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.e("Main","请求失败");

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

                String newsDataJsonstr=response.body().string();
             News    news=new Gson().fromJson(newsDataJsonstr,News.class);


                Log.e("main",""+news.getResult().getData().size());
                Message msg=Message.obtain();
                msg.what=1000;
                msg.obj=news;
                handler.sendMessage(msg);
//
//                Log.e("mian",news.getResult().getData().get(0).getTitle());


            }
        });

    }

    @Override
    public void start(String url) {
        Intent intent=new Intent(MainActivity.this,WebActivity.class);
        intent.putExtra("url",url);
        startActivity(intent);

    }
}
